#!/usr/bin/env python3

import requests
import urllib

## To use -- set globals below
## loads up a list of packages then inserts the names into magic URLs
## the release of interest was  August 14, 2021 -- let's do 6 months later to account for updates

urlStart = "https://qa.debian.org/cgi-bin/popcon-data?packages="
urlEnd = ";from_date=2022-02-15;to_date=2022-02-15"
theDate = "2022-02-15"
packageFile = "bullseye_packages.txt"
outFile = "bullseye_usage_20220215.tsv"
pkgList = []

with open(packageFile, 'r') as fh:
    pkgList = fh.readlines()
    pkgList = [line.strip() for line in pkgList] ## ooh fancy list comprehension

with open(outFile, 'w') as outfile:

    for pkg in pkgList:
        jResponse = requests.get(f"{urlStart}{urllib.parse.quote(pkg)}{urlEnd}") #encode pkg and get json back
        if jResponse.status_code == 200: #happy
            j = jResponse.json()
            try:
                j = j[pkg] # dig in by layers -- package, then date
                j = j[theDate]
                inst = j['no_files'] + j['old'] + j['recent'] + j['vote']
                outfile.write(f"{pkg}\t{inst}\n")
            except:
                print(f"Error for {pkg}, json is {j}")
        else:
            print(f"Failed to get {pkg} data with code {jResponse.status_code}")
